package com.gt.web.srv.utils;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import com.gt.web.api.exception.BusinessException;
import com.gt.web.srv.domain.BizStore;
import com.gt.web.srv.domain.Coupon;
import com.gt.web.srv.enums.UserStatusEnum;
import com.gt.web.srv.web.api.resource.*;
import com.gt.web.srv.web.api.resource.dto.LabelStoreDto;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Date;
/**
 * <p>
 * BizUtils
 * </p>
 *
 * @author: WeiHuan
 * @version:1.0
 * @date: 2024/6/1 11:00
 * 注意：本内容仅限于公司内部传阅，禁止外泄以及用于其他的商业目的
 */

public class BizUtils {

	/**
	 *
	 * @param quantum
	 * @return
	 */
	public static boolean checkTimeQuantum(String quantum) {
		DayOfWeek week = LocalDateTime.now().getDayOfWeek();
		Boolean normal = false;
		String[] split = quantum.split(",");
		Integer current = 0;
		for (int i = 0; i < split.length; i++) {
			current = Integer.valueOf(split[i])+1;
			if (current.equals(Integer.valueOf(week.getValue()))) {
				normal = true;
				break;
			}
		}
		return normal;
	}

	public static LabelStoreDto checkVerification(String stores, String code) {
		List<LabelStoreDto> labels = JSONUtil.toBean(stores, new TypeReference<List<LabelStoreDto>>() {
		}, true);
		LabelStoreDto current = null;
		for (LabelStoreDto label : labels) {
			if (label.getCode().equals(code)) {
				current = label;
			}
		}
		return current;
	}

	public static List<String> toTimes(String times) {
		List<String> time = new ArrayList<>();
		String[] split = times.split(",");
		for (int i = 0; i < split.length; i++) {
			switch (split[i]) {
				case "0": time.add("星期日");break;
				case "1": time.add("星期一");break;
				case "2": time.add("星期二");break;
				case "3": time.add("星期三");break;
				case "4": time.add("星期四");break;
				case "5": time.add("星期五");break;
				case "6": time.add("星期六");break;
			}
		}
		return time;
	}
	public static List<LabelStoreDto> toStore(String times) {
		List<LabelStoreDto> stores = JSONUtil.toBean(times, new TypeReference<List<LabelStoreDto>>() {
		}, true);
		return stores;
	}

	public static String bgColor(int i) {
		String[] colors = {"#63b359","#2c9f67","#509fc9","#5885cf","#9062c0","#d09a45","#e4b138","#ee903c","#f08500","#a9d92d","#dd6549","#cc463d","#cf3e36","#5E6671"};
		return colors[i];
	}
	public static String CouponDescription(Coupon coupon) {
		// todo 1-券有效期 有效期类型 0 :自领取日多少天内有效,1:开始&结束
		StringBuffer sb = new StringBuffer();
		sb.append("券信息：");
		// 类型 0:满减,1:打折券,2:兑换券
		if (coupon.getCouponType()==0) {
			sb.append("满"+coupon.getAmount()+"元，减"+coupon.getReduce()+"元");
		}
		else if (coupon.getCouponType() == 1) {
			sb.append("满"+coupon.getAmount()+"元，打"+coupon.getDiscount()+"折");
		}
		else if(coupon.getCouponType()==2){
			sb.append("满"+coupon.getAmount()+"元，兑换领取"+coupon.getExchangeItems());
		}
		sb.append("\n券有效期：");
		if (coupon.getPeriodType()==0) {
			sb.append("兑换当日+"+coupon.getDays()+"天");
		}
		else {
			sb.append(BizUtils.time2Str(coupon.getStartTime())+"-"+BizUtils.time2Str(coupon.getEndTime()));
		}
		sb.append(",过期作废");
		return sb.toString();
	}

	public static String store2str(List<BizStore> stores, String storeJson) {

		List<String> address = new ArrayList<>();
		List<LabelStoreDto> labelStoreDto = JSONUtil.toBean(storeJson, new TypeReference<List<LabelStoreDto>>() {
		}, true);
		for (BizStore store : stores) {
			for (LabelStoreDto storeDto : labelStoreDto) {
				if (store.getStoreId().equals(storeDto.getId())){
					address.add(store.getAddress());
				}
			}
		}
		return StringUtils.join(address,"、");
	}

	// public static ResponseUserVo userRole(ResponseUserVo userVo) {
	//
	// 	if (Objects.isNull(userVo.getPeopleInfo())){
	// 		userVo.setRole(0);
	// 		return userVo;
	// 	}
	//
	// 	if(Objects.nonNull(userVo.getPeopleInfo().getIsGrid()) && userVo.getPeopleInfo().getIsGrid()>0) {
	// 		userVo.setRole(1);
	// 	}
	// 	else if (Objects.nonNull(userVo.getPeopleInfo().getIsCommittee()) && userVo.getPeopleInfo().getIsCommittee()>0) {
	// 		userVo.setRole(2);
	// 	}
	// 	else {
	// 		userVo.setRole(0);
	// 	}
	// 	return userVo;
	// }

	public static LocalDateTime str2Time(String str) {
		DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		return LocalDateTime.parse(str, df);
	}
	public static String time2Str(LocalDateTime str) {
		String enterTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm").format(str);
		return enterTime;
	}
	public static LocalDateTime todayStart() {
		return LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
	}
	public static LocalDateTime todayEnd() {
		return LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
	}
	/**
	 * 用户身份的检查
	 */
	public static void validate(ResponseUserVo userVo) {

		List<Integer> states = Arrays.asList(UserStatusEnum.NO_REGISTER.getStatus()
				, UserStatusEnum.AUDIT_WAIT.getStatus()
				, UserStatusEnum.AUDIT_FAIL.getStatus()
				, UserStatusEnum.WAIT_USER_FILL.getStatus());
		if (states.contains(userVo.getUserStatus())) {
			throw new BusinessException(9000,"请先完成身份认证");
		}
	}



	public static  String datePath()
	{
		Date now = new Date();
		return DateFormatUtils.format(now, "yyyy/MM/dd")+"/";
	}
}